package com.yl.chainStore.control.user

import com.yl.chainStore.control.BaseCtrl
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import com.yl.users.common.util.deptListToTree
import com.yl.users.entity.CompanyDept
import com.yl.users.pojo.vo.CompanyDeptSelectVo
import com.yl.users.services.ICompanyDeptService
import com.yl.users.services.ISysCompanyService
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("company/dept")
open class CompanyDeptController(
	private val iSysCompanyService: ISysCompanyService,
	private val iCompanyDeptService: ICompanyDeptService
) : BaseCtrl() {

	/**
	 * 获取部门树形图
	 */
	@GetMapping("getDeptTree")
	@Transactional
	open fun getDeptTree(): ResponseBody {
		val deptId = getContext().subCompanyId
		val resList = mutableListOf<CompanyDept>()
		//当前分院
		val subCompanyDept = iCompanyDeptService.getDept(deptId)

		if(subCompanyDept == null){
			return returnSuccess()
		}else{
			resList.add(subCompanyDept)
		}
		subCompanyDept.children = deptListToTree(iCompanyDeptService.getSubCompanyDeptList(CompanyDeptSelectVo(
			subCompanyId = deptId
		)),deptId)

		//子分院处理
		val subCompanyList = iCompanyDeptService.getSubCompanyList(deptId)
		for(sub in subCompanyList){
			sub.children = deptListToTree(iCompanyDeptService.getSubCompanyDeptList(CompanyDeptSelectVo(
				subCompanyId = sub.id
			)),sub.id)
		}
		subCompanyDept.children!!.addAll(subCompanyList)
		return returnSuccess(resList)
	}

	/**
	 * 获取部门树形图(可能不用)
	 */
	@GetMapping("tree/{subCompanyId}")
	@Transactional
	open fun tree(@PathVariable subCompanyId:Long): ResponseBody {
		return returnSuccess(
			deptListToTree(
				iSysCompanyService.getCompanyDeptList(
					companyDept = CompanyDeptSelectVo(
						companyId = getContext().companyId,
						subCompanyId = subCompanyId
					)
				), 0L
			)
		)
	}

	@GetMapping("detail/{id}")
	@Transactional
	open fun detail(@PathVariable id: Long): ResponseBody {
		val companyDept = iCompanyDeptService.getDept(id)
			?: return returnError(ErrorCode.DATA_IS_NOT_FIND)
		if (companyDept.superDeptId != 0L) {
			companyDept.superDept = iCompanyDeptService.getDept(id)
		}
		return returnSuccess(companyDept)
	}

	/**
	 * 添加部门
	 */
	@PostMapping("add")
	@PermsCode("company:user:dept:add")
	@Transactional(rollbackFor = [Exception::class])
	open fun add(@RequestBody dept: CompanyDept): ResponseBody {
		val addSubDept = iCompanyDeptService.addSubDept(dept)
		if(addSubDept.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addSubDept
	}

	/**
	 * 修改部门
	 */
	@PostMapping("update")
	@PermsCode("company:user:dept:update")
	@Transactional(rollbackFor = [Exception::class])
	open fun update(@RequestBody dept: CompanyDept): ResponseBody {
		val updateDept = iCompanyDeptService.updateDeptInfo(dept)
		if(updateDept.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return updateDept
	}

	/**
	 * 删除部门
	 */
	@PostMapping("del/{id}")
	@PermsCode("company:user:dept:del")
	@Transactional
	open fun del(@PathVariable id: Long): ResponseBody {
		val delDept = iCompanyDeptService.delDept(id)
		if(delDept.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}

		return delDept
	}


}
